package com.yujy.logaspect.advicd; import com.alibaba.fastjson.JSON; import com.yujy.logaspect.annotation.OperateLog; import com.yujy.logaspect.test.bean.SysOperateLog; import com.yujy.logaspect.test.dao.SysOperateLogDao; import com.yujy.logaspect.vo.base.BaseRequest; import lombok.extern.slf4j.Slf4j; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.reflect.MethodSignature; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import javax.servlet.http.HttpServletRequest; import java.lang.reflect.Method; import java.math.BigDecimal; import java.util.Date; /** * ClassName: OperateLogAdvice <br/> * Description: TODO <br/> * Date: 2021/7/29 14:16<br/> * <br/> * * @author cornyu */ @Aspect @Component @Slf4j public class OperateLogAdvice { @Autowired SysOperateLogDao logDao; @Pointcut("execution( * com.yujy..controller.*.*(..))")//两个..代表所有子目录,最后括号里的两个..代表所有参数 public void logPointCut() { } @Before("logPointCut()") public void doBefore(JoinPoint joinPoint) throws Throwable { //接收到请求,记录请求内容 ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = attributes.getRequest(); //记录下请求内容 log.info("请求地址 : " request.getRequestURL().toString()); log.info("HTTP METHOD : " request.getMethod()); log.info("CLASS_METHOD : " joinPoint.getSignature().getDeclaringTypeName() "." joinPoint.getSignature().getName()); log.info("参数 : " JSON.toJSONString(joinPoint.getArgs())); } @AfterReturning(returning = "ret", pointcut = "logPointCut()")//returning的值和doAfterReturning的参数名一致 public void doAfterReturning(Object ret) throws Throwable { //处理完请求 log.info("返回值 : " ret); } @Around("logPointCut()") public Object doAround(ProceedingJoinPoint pjp) throws Throwable { long startTime = System.currentTimeMillis(); Object ob = pjp.proceed();//ob为方法的返回值 //执行时长(毫秒) long time = System.currentTimeMillis() - startTime; //保存 请求数据 saveLog(pjp, time); return ob; } private void saveLog(ProceedingJoinPoint joinPoint, long time) { try { MethodSignature signature = (MethodSignature) joinPoint.getSignature(); Method method = signature.getMethod(); //获取方法上的自定义注解,如果有标注 OperateLog则往下继续 OperateLog operateLogAnnotation = method.getAnnotation(OperateLog.class); if (operateLogAnnotation == null) { // 注解上的描述 return; } //请求的方法名 String className = joinPoint.getTarget().getClass().getName(); String methodName = signature.getName(); //接收到请求,记录请求内容 ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = attributes.getRequest(); String argsStr = JSON.toJSONString(joinPoint.getArgs()[0]); //将第一个请求参数 转为jsonstr BaseRequest baseRequest = JSON.parseObject(argsStr, BaseRequest.class); //请求的参数 保存到数据库中 SysOperateLog sysLog = new SysOperateLog(); String logKey = operateLogAnnotation.key(); sysLog.setCreatetime(new Date()); sysLog.setLogkey(logKey); sysLog.setMethodname(methodName); sysLog.setUsercode(baseRequest.getUserId()); sysLog.setRequestparam(argsStr); sysLog.setTime(BigDecimal.valueOf(time)); logDao.insertSelective(sysLog); } catch (Exception e) { log.error(e.getMessage()); e.printStackTrace(); } }}
.├── logaspect│ ├── HELP.md│ ├── logaspect.iml│ ├── mvnw│ ├── mvnw.cmd│ ├── pom.xml│ ├── src│ │ └── main│ │ ├── java│ │ │ └── com│ │ │ └── yujy│ │ │ └── logaspect│ │ │ ├── LogaspectApplication.java│ │ │ ├── advicd│ │ │ │ └── OperateLogAdvice.java│ │ │ ├── annotation│ │ │ │ └── OperateLog.java│ │ │ ├── config│ │ │ │ └── TestDataSourceConfig.java│ │ │ ├── controller│ │ │ │ └── TestController.java│ │ │ ├── test│ │ │ │ ├── bean│ │ │ │ │ └── SysOperateLog.java│ │ │ │ └── dao│ │ │ │ ├── SysOperateLogDao.java│ │ │ │ └── SysOperateLogDao.xml│ │ │ └── vo│ │ │ └── BaseRequest.java│ │ └── resources│ │ ├── application.properties│ │ ├── db-test-dev.properties│ │ ├── sql│ │ │ └── v1.sql│ │ ├── static│ │ └── templates│ └── target│ ├── classes│ │ ├── application.properties│ │ ├── com│ │ │ └── yujy│ │ │ └── logaspect│ │ │ ├── LogaspectApplication.class│ │ │ ├── advicd│ │ │ │ └── OperateLogAdvice.class│ │ │ ├── annotation│ │ │ │ └── OperateLog.class│ │ │ ├── config│ │ │ │ └── TestDataSourceConfig.class│ │ │ ├── controller│ │ │ │ └── TestController.class│ │ │ ├── test│ │ │ │ ├── bean│ │ │ │ │ └── SysOperateLog.class│ │ │ │ └── dao│ │ │ │ ├── SysOperateLogDao.class│ │ │ │ └── SysOperateLogDao.xml│ │ │ └── vo│ │ │ └── BaseRequest.class│ │ └── db-test-dev.properties│ └── generated-sources│ └── annotations└── 找例子网_logaspect.rar34 directories, 29 files
评论